package com.base.cn.platform.os.service.course.cou.buy;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.course.cou.buy.CourseBuyPackageCourseDao;
import com.base.cn.platform.os.dao.course.cou.buy.CourseBuyRecordDao;
import com.base.cn.platform.os.entity.course.cou.Course;
import com.base.cn.platform.os.entity.course.cou.buy.CourseBuyPackageCourse;
import com.base.cn.platform.os.service.course.cou.CourseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 套餐激活Biz
 *
 * @author s.li
 * @create 2018-05-17-14:41
 */
@Service
public class CourseBuyPackageCourseBiz extends BaseBiz<CourseBuyPackageCourse,CourseBuyPackageCourseDao> {

    @Autowired
    private CourseBiz courseBiz;
    @Autowired
    private CourseBuyRecordDao  courseBuyRecordDao;

    /**
     * 生成套餐激活记录对象
     * @param orderId
     * @param orderNo
     * @param userId
     * @param packageId
     * @param courseId
     * @return CourseBuyPackageCourse
     */
    public CourseBuyPackageCourse getCourseBuyPackageCourse(BigDecimal orderId,String orderNo,BigDecimal userId,BigDecimal packageId,BigDecimal courseId){
        CourseBuyPackageCourse courseBuyPackageCourse = new CourseBuyPackageCourse();
        courseBuyPackageCourse.setOrderId(orderId);
        courseBuyPackageCourse.setOrderNo(orderNo);
        courseBuyPackageCourse.setUserId(userId);
        courseBuyPackageCourse.setPackageId(packageId);
        courseBuyPackageCourse.setCourseId(courseId);
        return courseBuyPackageCourse;
    }

    /**
     * 添加激活记录
     * @param courseBuyPackageCourseList
     */
    public void createCourseBuyPackageCourse(List<CourseBuyPackageCourse> courseBuyPackageCourseList){
        this.batchSave(courseBuyPackageCourseList);
    }

    /**
     *通过套餐ID串，查询套餐下的激活过的课程数据列表
     *@param packageIds 套餐ID串
     *@param userId 用户id
     *@param setCourseData 是否设置课程数据
     *@return List<CourseBuyPackageCourse>
     */
    public List<CourseBuyPackageCourse> findCourseBuyPackageCourseByPackageIds(String packageIds,BigDecimal userId,boolean setCourseData){
        if(StringUtils.isNotEmpty(packageIds)){
            CourseBuyPackageCourse courseBuyPackageCourse = new CourseBuyPackageCourse();
            courseBuyPackageCourse.setPackageIds(packageIds);
            courseBuyPackageCourse.setUserId(userId);
            List<CourseBuyPackageCourse> courseBuyPackageCourseList = this.findCourseBuyPackageCourseList(courseBuyPackageCourse,setCourseData);
            return courseBuyPackageCourseList;
        }
        return null;
    }

    /**
     * 查询套餐激活列表
     * @param packageCourse
     * @param setCourseData
     * @return List<CourseBuyPackageCourse>
     */
    public List<CourseBuyPackageCourse> findCourseBuyPackageCourseList(CourseBuyPackageCourse packageCourse,boolean setCourseData){
        List<CourseBuyPackageCourse> packageCourseList = this.find(this.getWhereSql(packageCourse),null,null);
        return this.setCourseData(packageCourseList,setCourseData);
    }

    //=======================

    private String getWhereSql(CourseBuyPackageCourse courseBuyPackageCourse){
        StringBuilder whereSql = new StringBuilder("1=1");
        if(ObjectUtils.isNotEmpty(courseBuyPackageCourse)){
            if(StringUtils.isNotEmpty(courseBuyPackageCourse.getOrderNo())){
                whereSql .append(" and orderNo ='").append(courseBuyPackageCourse.getOrderNo()).append("'");
            }
            if(DataUtil.idIsNotNull(courseBuyPackageCourse.getOrderId())){
                whereSql .append(" and orderId = ").append(courseBuyPackageCourse.getOrderId());
            }
            if(DataUtil.idIsNotNull(courseBuyPackageCourse.getUserId())){
                whereSql .append(" and userId = ").append(courseBuyPackageCourse.getUserId());
            }
            if(StringUtils.isNotEmpty(courseBuyPackageCourse.getPackageIds())){
                whereSql .append(" and packageId in (").append(courseBuyPackageCourse.getPackageIds()).append(")");
            }
            if(DataUtil.idIsNotNull(courseBuyPackageCourse.getPackageId())){
                whereSql .append(" and packageId = ").append( courseBuyPackageCourse.getPackageId());
            }
        }
        return whereSql.toString();
    }

    /**
     * 设置课程数据
     * @param courseBuyPackageCourseList
     * @param setCourseData
     * @return List<CourseBuyPackageCourse>
     */
    private List<CourseBuyPackageCourse> setCourseData(List<CourseBuyPackageCourse> courseBuyPackageCourseList,boolean setCourseData){
        if(ObjectUtils.isNotEmpty(courseBuyPackageCourseList) && setCourseData){
            String courseIds = courseBuyPackageCourseList.stream().map(e->e.getCourseId().toString()).distinct().collect(Collectors.joining(","));
            Map<BigDecimal,Course> courseMaps =courseBiz.findCourseToMapByIds(courseIds,false,false,false);
            if(ObjectUtils.isNotEmpty(courseMaps)){
                courseBuyPackageCourseList.forEach(e->{
                    Course course = courseMaps.get(e.getCourseId());
                    e.setCourse(course);
                });
            }
        }
        return courseBuyPackageCourseList;
    }

    public String getUserSubjectIds(BigDecimal userId) {
        List<String> subjectList = courseBuyRecordDao.findUserSubjectByBuyCourse(userId);
        List<String> collect = subjectList.stream().flatMap(s -> Arrays.stream(s.split(","))).filter(e -> StringUtils.isNotEmpty(e)).collect(Collectors.toList());
        TreeSet<String> subject = new TreeSet<>((a, b)->a.compareTo(b));
        subject.addAll(collect);
        String subjects = String.join(",", subject);
        return subjects;
    }
}
